package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualitySeamRateBo;
import com.ruoyi.quality.domain.vo.QualitySeamRateImportVo;
import com.ruoyi.quality.domain.vo.QualitySeamRateVo;
import com.ruoyi.quality.domain.vo.RecentHalfYearSeamVo;
import com.ruoyi.quality.listener.QualitySeamRateImportListener;
import com.ruoyi.quality.service.IQualitySeamRateService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 焊缝探伤信息
 *
 * @author ruoyi
 * @date 2024-01-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/seamRate")
public class QualitySeamRateController extends BaseController {

    private final IQualitySeamRateService iQualitySeamRateService;

    /**
     * 查询焊缝探伤信息列表
     */
    @SaCheckPermission("quality:seamRate:list")
    @GetMapping("/list")
    public TableDataInfo<QualitySeamRateVo> list(QualitySeamRateBo bo, PageQuery pageQuery) {
        return iQualitySeamRateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出全部焊缝探伤信息列表
     */
    @SaCheckPermission("quality:seamRate:export")
    @Log(title = "焊缝探伤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(QualitySeamRateBo bo, HttpServletResponse response) {
        List<QualitySeamRateVo> list = iQualitySeamRateService.queryList(bo);
        ExcelUtil.exportExcel(list, "焊缝探伤信息", QualitySeamRateVo.class, response);
    }

    /**
     * 导出选择焊缝探伤信息列表
     */
    @SaCheckPermission("quality:seamRate:export")
    @Log(title = "焊缝探伤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{ids}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> ids, HttpServletResponse response) {
        List<QualitySeamRateVo> list = iQualitySeamRateService.queryByIds(ids);
        ExcelUtil.exportExcel(list, "焊缝探伤信息", QualitySeamRateVo.class, response);
    }

    /**
     * 获取焊缝探伤信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:seamRate:query")
    @GetMapping("/{id}")
    public R<QualitySeamRateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iQualitySeamRateService.queryById(id));
    }

    /**
     * 新增焊缝探伤信息
     */
    @SaCheckPermission("quality:seamRate:add")
    @Log(title = "焊缝探伤信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualitySeamRateBo bo) {
        return toAjax(iQualitySeamRateService.insertByBo(bo));
    }

    /**
     * 修改焊缝探伤信息
     */
    @SaCheckPermission("quality:seamRate:edit")
    @Log(title = "焊缝探伤信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualitySeamRateBo bo) {
        return toAjax(iQualitySeamRateService.updateByBo(bo));
    }

    /**
     * 删除焊缝探伤信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:seamRate:remove")
    @Log(title = "焊缝探伤信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iQualitySeamRateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "项目得分",  QualitySeamRateImportVo.class, response);
    }

    /**
     * 导入焊缝探伤信息数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "焊缝探伤信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("quality:seamRate:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<QualitySeamRateImportVo> result = ExcelUtil.importExcel(file.getInputStream(), QualitySeamRateImportVo.class, new QualitySeamRateImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询质量问题列表
     */
    @SaCheckPermission("quality:seamRate:list")
    @GetMapping("/getRecentHalfYearSeam")
    public List<RecentHalfYearSeamVo> getRecentHalfYearSeam(QualitySeamRateBo bo) {
        return iQualitySeamRateService.getRecentHalfYearSeam(bo);
    }

    /**
     * 计算指定条件下的焊缝总长、缺陷总长和相应合格率。
     *
     * @param bo 查询条件对象，包含指定人员、开始日期和结束日期
     * @return 包含焊缝总长、缺陷总长和相应合格率的Map，键为 "totalLength"、"defectLength" 和 "passRate"
     */
    @SaCheckPermission("quality:seamRate:list")
    @GetMapping("/calculateSeamRate")
    public Map<String, Object> calculateSeamRate(QualitySeamRateBo bo) {
        return iQualitySeamRateService.calculateSeamRate(bo);
    }
}
